<?php
///
namespace lxsphp\Core\Mvc;

///
use lxsphp\App;

/**
 * 框架 - 视图类
 *
 * @version $Id: View.php 195 2014-07-18 09:35:22Z linsir123 $
 * @package lxsphp\Core\Mvc
 */
class View
{
	/**
	 * 配置
	 *
	 * @var array
	 */
	public $conf = array();

	/**
	 * 模板引擎
	 *
	 * @var \Smarty
	 */
	public $engine;

	/**
	 * 模板列表
	 *
	 * @var array
	 */
	private $_list = array();

	/**
	 * 构造函数
	 */
	public function __construct()
	{
		///
		$this->conf = App::o("app.view");

		///
		if ($this->conf['engine'] == 'Smarty')
			$this->_initSmarty();
	}

	/**
	 * Smarty的display方法
	 */
	public function render()
	{
		///
		if (! empty($this->engine) && ! empty($this->_list)) {
			foreach ($this->_list as $v) {
				list($tpl, $cacheId) = $v;
				$this->engine->display($tpl, $cacheId);
			}
		}
	}

	/**
	 * Smarty的assign方法
	 *
	 * @param string $name 视图层的变量名
	 * @param mixed $val 待传送到视图层的数据
	 */
	public function assign($name, $val)
	{
		///
		if (! empty($this->engine))
			$this->engine->assign($name, $val);
	}

	/**
	 * 初始化模板信息
	 *
	 * @param string $tpl 模板文件名
	 * @param string $cacheId 当开启缓存时的缓存ID
	 */
	public function display($tpl, $cacheId = '')
	{
		///
		$this->_list[] = array($tpl, $cacheId);
	}

	/**
	 * 判断模板是否缓
	 *
	 * @param string $tpl 模板文件名
	 * @param string $cacheId 当开启缓存时的缓存ID
	 * @param string $suffix 模板文件后缀
	 *
	 * @return bool
	 */
	public function isCached($tpl, $cacheId = '', $suffix = '.html')
	{
		$tpl .= $suffix;

		///
		$this->display($tpl, $cacheId);

		///
		$this->engine->caching = $this->conf['smarty']['caching'];
		$this->engine->cache_lifetime = $this->conf['smarty']['cache_lifetime'];

		///
		return $this->engine->isCached($tpl, $cacheId);
	}

	/**
	 * 提供Smarty中构造链接的扩展函数
	 *
	 * 使用说明：其中a和c分别为控制器和控制器方法键值，不能随便改变
	 *
	 * @example <{url r="index/index" arg1="" arg2="" ...}>
	 *
	 * @param array $params 参数列表
	 *
	 * @return string
	 */
	public function smartyUrl($params)
	{
		///
		$route = isset($params['r']) ? trim($params['r']) : '';
		$params = array_diff($params, array($route));

		///
		return App::$I->getRequest()->createRequest($route, $params);
	}

	/**
	 * 初始化Smarty
	 */
	private function _initSmarty()
	{
		///
		App::v('smarty');

		///
		$relative = $this->_getRelDir();
		$templateDir = APP_DIR . 'Template' . $relative;
		$cacheDie = CACHE_DIR . 'runtime/template/cache' . $relative;
		$compileDir = CACHE_DIR . 'runtime/template/compile' . $relative;

		///
		$this->engine = App::$I->singleton('Smarty');
		$this->engine->left_delimiter = '<{';
		$this->engine->right_delimiter = '}>';
		$this->engine->use_sub_dirs = $this->conf['smarty']['use_sub_dirs'];
		$this->engine->setTemplateDir($templateDir);
		$this->engine->setCompileDir($compileDir);
		$this->engine->setCacheDir($cacheDie);

		///
		$this->engine->assign('domain', App::$I->getRequest()->getRelRootPath());
		$this->engine->registerPlugin('function', 'url', array(& $this, 'smartyUrl'));

		///
		App::$I->getLog()->mkdir($cacheDie);
		App::$I->getLog()->mkdir($compileDir);
	}

	/**
	 * 取得目录的相对路径
	 *
	 * @return string
	 */
	private function _getRelDir()
	{
		list($app,) = App::$I->alias;

		///
		return (! empty($app) ? ('/' . $app) : '');
	}
}
